package com.zh.common.view

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.view.isVisible
import com.zh.common.R
import com.zh.common.ktx.dp2px
import com.zh.common.ktx.inflate

class DownSeekView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

    private val iView by lazy { context.inflate(R.layout.view_seek) }
    private val textView by lazy { iView.findViewById<TextView>(R.id.tvProgress) }
    private val progressBar by lazy { iView.findViewById<ProgressBar>(R.id.progressBar) }

    init {
        addView(iView, LayoutParams(60f.dp2px, 30f.dp2px))
    }

    //进度0~1
    fun setProgress(progress: Float) {
        progressBar.progress = (progress * 100).toInt()
        if (progress == 1f ) {
            progressBar.isVisible = false
            setText("安装")
        } else {
            progressBar.isVisible = true
            setText("${(progress * 1000).toInt() / 10f}%")
        }
    }

    fun setInitProgress(progress: Float) {
        progressBar.progress = (progress * 100).toInt()
        progressBar.isVisible = true
    }

    fun setText(textSrc: String) {
        textView.text = textSrc
    }
}